import { createRouter, createWebHashHistory } from 'vue-router'
import { useUser } from "../store/user";
import { KeepAlive } from 'vue';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            name:'home',
            component: () => import('../views/home.vue'),
            meta: {
                label: '首页',
                content: {
                    keywords: '创屋岩板，传承国际流行的美学精髓',
                    description: '岩板、大板、创屋岩板',

                },
                KeepAlive: true,
            },
            //redirect: '/',//重定向
            children: [
                {
                    path: '/',
                    name:'index',
                    component: () => import('../views/index.vue'),
                    meta: {
                        label: '首页',
                        content: {
                            keywords: '创屋岩板，传承国际流行的美学精髓',
                            description: '岩板、大板、创屋岩板'
                        },
                        KeepAlive: true,
                    },
                },
                {
                    path: '/login',
                    component: () => import('../views/login.vue'),
                    meta: {
                        label: "登录"
                    }
                },
                {
                    path: '/mine',
                    component: () => import('../views/mine/index.vue'),
                    meta: {
                        label: '我的账户'
                    }
                },
                {
                    path: '/join',
                    component: () => import('../views/join/index.vue'),
                    redirect: '/join-list',
                    meta: {
                        label: '加入我们'
                    },
                    children: [
                        {
                            path: '/join-list',
                            component: () => import('../views/join/join-list.vue'),
                            meta: {
                                label: '热门岗位'
                            }
                        },
                        {
                            path: '/job-detail',
                            component: () => import('../views/join/job-detail.vue'),

                        }
                    ]
                },
                {
                    path: '/search-guide',
                    component: () => import('../views/search-guide.vue'),
                    meta: {
                        label: '搜索指南'
                    }
                },
                {
                    path: '/notice',
                    component: () => import('../views/notice/index.vue'),
                    meta: {
                        label: '公告资讯'
                    },
                    redirect: '/notice-list',
                    children: [
                        {
                            path: '/notice-list',
                            component: () => import('../views/notice/notice-list.vue'),
                        },
                        {
                            path: '/notice-detail',
                            component: () => import('../views/notice/notice-detail.vue'),
                        }
                    ]
                },
                {
                    path: '/goods-detail',
                    component: () => import('../views/goods-detail/index.vue'),
                    meta: {
                        label: '商品详情'
                    }
                },
                {
                    path: '/cart',
                    component: () => import('../views/cart/index.vue'),
                    meta: {
                        label: '购物车'
                    }
                },
                {
                    path: '/order',
                    component: () => import('../views/order/index.vue'),
                    meta: {
                        label: '订单'
                    }
                }
            ]
        },

    ]
})

let pathArr = ['/index', '/login', '/order', '/', '/notice-list', '/notice-detail', '/search-guide', '/join-list', '/job-detail']
router.beforeEach((to, from, next) => {
    let user = useUser()
    user.getAuths()
    if (pathArr.includes(to.path)) {
        next()
    } else {

        if (user.userinfo) {
            next()
        } else {
            next('/login')
        }
    }
})
export default router